package com.link.serve.utils;

import com.link.serve.tcp.message.Message;
import com.link.serve.tcp.message.proto.PackData;
import com.link.serve.tcp.message.proto.PackHeader;

/**
 * 消息封装工具类
 * @Author: 杰
 * @Project: link-server
 * @DateTime: 2025-01-25
 */
public class MessageUtils {

    /**
     * 构造响应消息
     * @param appId 应用 ID
     * @param uid 用户 ID
     * @param responseData 响应数据内容
     * @param messageType 消息类型
     * @return 封装好的响应消息
     */
    public static Message buildResponseMessage(int appId, long uid, String responseData, int messageType) {
        // 构造响应头部
        PackHeader.Header responseHeader = PackHeader
                .Header
                .newBuilder()
                .setAppId(appId) // 设置响应的业务 ID
                .setUid(uid) // 设置用户 ID
                .setMessageType(messageType) // 设置响应类型
                .build();

        // 构造响应体
        PackData.Body responseBody = PackData.Body.newBuilder()
                .setData(responseData) // 设置响应消息体内容
                .build();

        // 封装消息对象
        Message response = new Message()
                .setPacketBoundary(0xabcd)
                .setVersion(1)
                .setPacketHeaderLength(responseHeader.toByteArray().length)
                .setPacketDataLength(responseBody.toByteArray().length)
                .setHeader(responseHeader)
                .setBody(responseBody);

        return response;
    }
}
